<?php 
require 'header.php';
echo "<h3>Payroll</h3>";

if(isset($_POST['hours']))
{
echo "<h4>Hours Updated</h4>";
$query="SELECT * FROM employee";
$result=mysqli_query ($con, $query);
	while($emp = mysqli_fetch_array($result))
	{
	$id=$emp['ID'];
	$newTime=$_POST['hours'.$id]+($_POST['min'.$id]/60);
	$query1="UPDATE employee SET monthly_hours=monthly_hours+$newTime WHERE ID=".$id;
	if (!mysqli_query($con,$query1))
	{
	die('Error: ' . mysqli_error($con));
	}
	}

}
if(isset($_POST['pay']))
{
echo "<h4>Employees Paid</h4>";
$query="SELECT * FROM employee";
$result=mysqli_query ($con, $query);

	$query="SELECT * FROM employee";
	$result=mysqli_query ($con, $query);
	
	echo "<table border=1 cellpading=10>";

	echo "<tr><th>Employee Name</th><th>Total Hours</th><th>Hourly Salary</th><th>Amount Paid</th></tr>";
	while($emp = mysqli_fetch_array($result))
	{
		echo "<tr><td>$emp[employee_name]</td><td>$emp[monthly_hours]</td><td>$emp[hourly_salary]</td><td>$" . sprintf('%0.2f',$emp['hourly_salary']*$emp['monthly_hours']) . "</td>"; 
	}
	
	echo "</table>";
	$query="SELECT * FROM employee";
	$result=mysqli_query ($con, $query);
	while($emp = mysqli_fetch_array($result))
	{
		$id=$emp['ID'];
		$query="UPDATE employee SET monthly_hours=0 WHERE ID=".$id;
		if (!mysqli_query($con,$query))
		{
			die('Error: ' . mysqli_error($con));
		}
	}
}else
{
	$query="SELECT * FROM employee";
	$result=mysqli_query ($con, $query);
	echo '<form action="" method="post">';
	echo "<table border=1 cellpading=10>";
	
	echo "<tr><th>Employee Name</th><th>Total Hours</th><th>Add Hours</th><th>Add Minutes</th></tr>";
	while($emp = mysqli_fetch_array($result))
	
	{
		$hours="hours".$emp['ID'];
		$min="min".$emp['ID'];
		echo "<tr><td>$emp[employee_name]</td><td>$emp[monthly_hours]</td><td><input type='text' name=$hours size='4' value='0'></td><td><input type='text' name=$min size='4' value='0'></td></tr>"; 
	}
	
	echo "</table>";
	echo '<input type="submit" name="hours" value="Update Hours">';
	echo '<input type="submit" name="pay" value="Pay Employees">';
	echo '</form>';
	}
	




require 'footer.php';					
?>
	
